package com.ruoyi.project.system.device.pdf;
 

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
 
/**
 * 模板第一页不占满，生成表格
 */
public class PDFExport {
    /**
     * 获取上传根目录
     * @return
     */
    public static final String getBaseDir(){
        return "/pdf";//ProjectConfig.getDownloadPath();
    }
 
    /**
     * 获取模板
     * @return
     */
    public static final ClassPathResource getTpl(){
        return new ClassPathResource("/pdf/111.pdf");
    }
 
    /**
     * 编码文件名
     * @return
     */
    public static final String extractFilename()
    {
        return String.format("pdf/%s/%s.%s", "20240728", "1","pdf");
    }
 
    /**
     * 获取URL
     * @param fileName
     * @return
     */
    public static final String getPathFileName(String fileName) throws IOException {
        return "localhost:80/"+getBaseDir()+fileName;
    }
 
    /**
     * 获取下载绝对路径
     * @param fileName
     * @return
     */
    public static final java.io.File getAbsoluteFile(String fileName)
    {
        java.io.File desc = new java.io.File(getBaseDir() + java.io.File.separator + fileName);
 
        if (!desc.exists())
        {
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
            try {
                desc.createNewFile();
 
            } catch (IOException e) {
 
                e.printStackTrace();
            }
        }
        return desc;
    }
    /**
     * 单行数据写入
     * //隐藏上边框
     * // cell.disableBorderSide(1);
     * //隐藏下边框
     * // cell.disableBorderSide(2);
     * //隐藏上、下边框
     * //cell.disableBorderSide(3);
     * //隐藏左边框
     * //cell.disableBorderSide(4);
     * //隐藏左、上边框
     * //cell.disableBorderSide(5);
     * //隐藏左、下边框
     * //cell.disableBorderSide(6);
     * //隐藏左、上、下边框
     * //cell.disableBorderSide(7);
     * //隐藏右边框
     * //cell.disableBorderSide(8);
     * //隐藏右、上边框
     * // cell.disableBorderSide(9);
     * //隐藏右、下边框
     * //cell.disableBorderSide(10);
     * //隐藏右、上、下边框
     * //cell.disableBorderSide(11);
     * //隐藏左、右边框
     * //cell.disableBorderSide(12);//左右没了
     * //隐藏上、左、右边框
     * //cell.disableBorderSide(13);//只剩下
     * //隐藏下、左、右边框
     * //cell.disableBorderSide(14);//只剩上
     * //隐藏全部
     * //cell.disableBorderSide(15);//全没了
     * @param table
     * @param setRowspan
     * @param setColspan
     * @param text
     * @param disableBorderSide
     * @throws IOException
     * @throws DocumentException
     */
    public static void setRowData(PdfPTable table,int setRowspan,int setColspan,String text,int disableBorderSide) throws IOException, DocumentException {
        //定义数据的字体
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font textFont = new Font(baseFont, 10, Font.NORMAL);
        PdfPCell heandCell = new PdfPCell();
        heandCell.setRowspan(setRowspan);
        heandCell.setColspan(setColspan);
        heandCell.setMinimumHeight(30);
        heandCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        heandCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        heandCell.setPhrase(new Phrase(text, textFont));
        if(disableBorderSide>0){
            heandCell.disableBorderSide(disableBorderSide);//隐藏边框
        }
        table.addCell(heandCell);
    }
 
    /**
     * 单行数据写入
     * @param table
     * @param setRowspan
     * @param setColspan
     * @param text
     * @throws IOException
     * @throws DocumentException
     */
    public static void setRowData(PdfPTable table,int setRowspan,int setColspan,String text) throws IOException, DocumentException {
        setRowData(table,setRowspan,setColspan,text,-1);
    }
 
    /**
     * 生成表格
     * @param writer
     * @param colls
     * @return
     */
    public static PdfPTable getTable(PdfWriter writer,float[] colls){
        PdfPTable table = new PdfPTable(colls);
        table.setTotalWidth(520);
        table.setPaddingTop(0);
        table.setLockedWidth(true);
        table.setHorizontalAlignment(Element.ALIGN_CENTER);//居中
        table.writeSelectedRows(0, -1, 500, 800, writer.getDirectContentUnder());
        return table;
    }
}
 
 